<?php

/**
 * Created by PhpStorm.
 * User: 王军
 * Date: 2017/3/22 0022
 * Time: 上午 11:37
 */
namespace Admin\Controller;
use Think\Controller;
class CpxlController extends Controller{
    protected $model;
    public function __construct(){
        parent::__construct();
        $this->model = D('xl_expend');
    }

    //添加类型信息
    public function add(){
        if(!IS_POST){
            //取出分类数据
            $cats = $this->model->getcatinfo();
            //取出电参数ll
            $diancanshuinfo = $this->model->getdiancanshuAll();
            $this->assign(array(
                'title' => '添加系列扩展信息',
                'buturl' => U('Cpxl/lis'),
                'butname' => '系列扩展信息列表'
            ));
            $this->assign(array(
                'cats' => $cats,
                'diancanshu' => $diancanshuinfo
            ));
            $this->display();
        }else{
            //dump(I('post.'));exit;
            set_time_limit(0);
            if(!$this->model->create(I('post.'),1)){
                $this->error($this->model->getError());
            }else{
                if(!$this->model->add()){
                    $this->error('添加失败');
                }else{
                    $this->success('添加成功',U('lis'));
                }
            }
        }
    }

    //ajax取出对应的最后一级分类
    public function ajax_getcatlast(){
        $flno = I('get.flno',0,'intval');
        $catlast = $this->model->getcatlast($flno);
        $this->ajaxReturn($catlast);
    }
    //ajax取出对应的系列
    public function ajax_getcatxl(){
        $flno = I('get.flno',0,'intval');
        $info = $this->model->getcatxl($flno);
        $this->ajaxReturn($info);
    }
    //ajax获取当前系列所属的分类的电参数
    public function ajax_getdiancanshu(){
        $flno = I('get.flno');
        //$flno = 266;
        $info = $this->model->getdiancanshu($flno);
        //dump($info);exit;
        $this->ajaxReturn($info);
    }

    //系列扩展信息列表
    public function lis(){
        $info = $this->model->search();
        //dump($info);exit;
        $this->assign(array(
            'title' => '系列扩展信息列表',
            'buturl' => U('Cpxl/add'),
            'butname' => '添加系列扩展信息'
        ));
        $this->assign(array(
            'list' => $info['list'],
            'page' => $info['page_str']
        ));
        $this->display();
    }

    //单个系列扩展信息展示
    public function xl_intro(){
        $cpxlno = I('get.cpxlno');
        $info = $this->model->search_one($cpxlno);
        //dump($info);exit;
        $this->assign(array(
            'title' => '系列扩展信息详情',
            'buturl' => U('Cpxl/lis'),
            'butname' => '系列扩展信息列表'
        ));
        $this->assign(array(
            'info' => $info,

        ));
        $this->display();
    }

    //系列信息维护
    public function edit(){
        $cpxlno = I('get.cpxlno');
        if(!IS_POST){
            $info = $this->model->search_one($cpxlno);
            //取出电参数ll
            $diancanshuinfo = $this->model->getdiancanshuAll();
            //dump($info);exit;
            $this->assign(array(
                'title' => '维护系列扩展信息',
                'buturl' => U('Cpxl/lis'),
                'butname' => '系列扩展信息列表',
            ));
            $this->assign(array(
                'info' => $info,
                'diancanshu' => $diancanshuinfo
            ));
            $this->display();
        }else{
            //dump(I(('post.')));exit;
            set_time_limit(0);
            if(!$this->model->create(I('post.'),2)){
                $this->error($this->model->getError());
            }else{
                if(false === $this->model->save()){
                    $this->error('修改失败');
                }else{
                    $this->success('修改成功',U('lis'));
                }
            }
        }
    }


    //ajax点击删除产品的图片和数据
    public function ajax_remove(){
        $id = I('get.id');
        $dbname = I('get.dbname');
        if($dbname == 'xl_img'){
            $img = 'img';
        }else if($dbname == 'size_img'){
            $img = 'ct_img';
        }else if($dbname == 'xl_other_img'){
            $img = 'qt_img';
        }
        $db_model = D($dbname);
        $info = $db_model->find($id);
        $type = $db_model->delete($id);
        $path = C('IMAGE_CONFIG.ROOTPATH');
        //$this->ajaxReturn($path.$imgs['img']);
        if( $type && unlink($path .$info[$img]) ){
            $this->ajaxReturn(true);
        }else{
            $this->ajaxReturn(false);
        }
    }

    //删除扩展信息
    public function del(){
        $id = I('get.id');
        if($this->model->delete($id)){
            $this->success('删除成功',U('lis'));
        }else{
            $this->error('删除失败');
        }
    }



}